﻿var selectedRowId = { Id: '-1' };
var idsOfSelectedRows = [];
var gridData = '#grdData';

//============================================
$(document).ready(function () {

    $(".datepicker").datepicker({
        showOn: "button",
        buttonImage: dateIconURL,
        changeMonth: true,
        changeYear: true,
        buttonImageOnly: true,
        dateFormat: 'mm/dd/yy',
        constrainInput: true
    });

    SetupGrid(GetgridOptions(), GetColumnNames, GetColumnModel, selectedRowId, idsOfSelectedRows, FillData, GetDataPosted('#grdData'), null, null);

    $('#btnRefresh').click(function () {
        //alert(tinymce.get('Contents').getContent());
        ClearText();
    });

    $('#btnFind').click(function () {
        TriggerReloadDataGrid(gridData);
    });
    
});

//============================================
function browseImageOnChange() {
    //$('#ImageContentUrl').val($('#browseImage').val());
    var listStr = $('#browseImage').val().split('\\');
    if (listStr.length > 0) {
        $('#ImageContentUrl').val(listStr[listStr.length - 1]);
    }
}

//----------func cleartext()----------------------------------
function ClearText() {
    $('#Condition_Find').val("");
    $('#UserName').val("");
    $('#DisplayName').val("");
    $('#UserPass').val("");
    $('#Email').val("");
    $('#CreationDate').val("");
    $('#UserID').val("0");
    $('#IsActive').attr('checked', true);

}
//------------------------

//========func GetgridOption====================================
function GetgridOptions() {
    var obj = new Object();

    obj.Id = "#grdData";
    obj.Url = GetAllInformPath;
    obj.Pager = "#pager";
    obj.IsAutoWidthColumn = false;
    obj.IsMultiSelect = false;
    obj.ColumnKeyIndex = "CreationDate";
    obj.DefaultSortName = "CreationDate";
    obj.DefaultSortType = "date";
    obj.DefaultSortOrder = "desc";
    //ClearText();
    /*
    $('#Condition_Find').val("");
    $('#Subject').val("");
    $('#ImageContentUrl').val("");
    $('#ShortcutText').val("");
    $('#CreationDate').val("");
    //tinymce.get('Contents').setContent('<p>Nhập nội dung tin</p>');
    $('#NewsID').val("0");
    */
    return obj;
}
//--------------------------------------------

//============================================
function GetColumnNames() {
    return ['Mã User', 'Tên đăng nhập', 'Quyền', 'Quyền', 'Tên đầy đủ', 'Email', 'Mật khẩu', 'Kích hoạt', 'Khóa', 'Ngày tạo'];
}
//--------------------------------------------

//============================================
function GetColumnModel() {
    return [
            { name: 'UserID', index: 'UserID', hidden: true },
            { name: 'UserName', index: 'UserName', width: 100, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'RoleID', index: 'RoleID', hidden: true },
            { name: 'RoleName', index: 'RoleName', width: 100, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'DisplayName', index: 'DisplayName', width: 250, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'Email', index: 'Email', width: 150, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'UserPass', index: 'UserPass', hidden: true },
            { name: 'IsActive', index: 'IsActive', width: 70, align: "left"},
            { name: 'Locked', index: 'Locked', width: 70, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'CreationDate', index: 'CreationDate', width: 70, align: "left", sortable: true }
            ];
}
//--------------------------------------------
//============================================
function FillData() {

    $('#UserID').val(GetCellValue('#grdData', selectedRowId.Id, 'UserID'));
    $('#UserName').val(GetCellValue('#grdData', selectedRowId.Id, 'UserName'));
    $('#RoleID').val(GetCellValue('#grdData', selectedRowId.Id, 'RoleID'));
    $('#DisplayName').val(GetCellValue('#grdData', selectedRowId.Id, 'DisplayName'));
    $('#Email').val(GetCellValue('#grdData', selectedRowId.Id, 'Email'));
    $('#UserPass').val(GetCellValue('#grdData', selectedRowId.Id, 'UserPass'));
    if (GetCellValue('#grdData', selectedRowId.Id, 'IsActive') == "True" || GetCellValue('#grdData', selectedRowId.Id, 'IsActive') == "1") {
        $('#IsActive').attr('checked', true)
    }
    else {
        $('#IsActive').attr('checked', false)
    }

    if (GetCellValue('#grdData', selectedRowId.Id, 'Locked') == "True" || GetCellValue('#grdData', selectedRowId.Id, 'Locked') == "1") {
        $('#Locked').attr('checked', true)
    }
    else {
        $('#Locked').attr('checked', false)
    }

    $('#CreationDate').val(GetCellValue('#grdData', selectedRowId.Id, 'CreationDate'));
    return;
}
//--------------------------------------------
//============================================
function GetDataPosted(list) {

    return {

        Condition_Find: function () { return $('#Condition_Find').val(); },

        sortName: function () { return $(list).jqGrid('getGridParam', 'sortname'); },
        sortOrder: function () { return $(list).jqGrid('getGridParam', 'sortorder'); },
        rowNumber: function () { return $(list).jqGrid('getGridParam', 'rowNum'); },
        rowId: function () {
            return (updateData == true ? selectedRowId.Id : -1);
        },
        page: function () { return $(list).jqGrid('getGridParam', 'page'); }
    };
}
//--------------------------------------------

